#include "hash_strings.h"

#include <stdlib.h>
#include <bstrlib.h>

unsigned long
    hash_from_string(void *str)
{
    unsigned long hash = 0;
    int c;
    int i;

    for (i=0;i<((bstring)str)->slen;i++)
    {
        c=((bstring)str)->data[i];
        hash = c + (hash << 6) + (hash << 16) - hash;
    }

    return hash;
}

int bstr_eq(void *k1, void *k2)
{
    if (biseqcaseless ((bstring)k1,(bstring)k2)==1)
        return 1;
    return 0;
}

#include "hashtable_private.h"

void
string_hashtable_destroy(struct hashtable *h, int free_values)
{
    unsigned int i;
    struct entry *e, *f;
    struct entry **table = h->table;
    if (free_values)
    {
        for (i = 0; i < h->tablelength; i++)
        {
            e = table[i];
            while (NULL != e)
            {
                f = e;
                e = e->next;
                bdestroy(f->k);
                bdestroy(f->v);
                free(f);
            }
        }
    }
    else
    {
        for (i = 0; i < h->tablelength; i++)
        {
            e = table[i];
            while (NULL != e)
            { f = e; e = e->next; bdestroy(f->k); free(f); }
        }
    }
    free(h->table);
    free(h);
}

